home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / LITTLE / P3SRC.ZIP / ATARI / USERIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-15  |  13.2 KB  |  566 lines

  1. /****************************************************************************
  2. *                userio.c
  3. *
  4. *  This module contains I/O routines.
  5. *
  6. *  from Persistence of Vision(tm) Ray Tracer
  7. *  Copyright 1996 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file. If
  14. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  16. *  Forum.  The latest version of POV-Ray may be found there as well.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23.  
  24. #include <stdarg.h>
  25. #include "frame.h"
  26. #include "vector.h"
  27. #include "povproto.h"
  28. #include "parse.h"
  29. #include "povray.h"
  30. #include "tokenize.h"
  31. #include "userio.h"
  32.  
  33. STREAM_INFO Stream_Info[MAX_STREAMS];
  34. static char *vsbuffer;
  35. static void PrintToStream PARAMS((int stream, char *s));
  36.  
  37. /****************************************************************************/
  38. /* Prints s to the stream's console or file destination, as specified by type */
  39.  
  40. static void PrintToStream(stream, s)
  41. int stream;
  42. char *s;
  43. {
  44.   if (Stream_Info[stream].handle != NULL)
  45.   {
  46.     fprintf(Stream_Info[stream].handle, s);
  47.     fflush(Stream_Info[stream].handle);
  48.   }
  49.  
  50.   if (Stream_Info[ALL_STREAM].handle != NULL)
  51.   {
  52.     fprintf(Stream_Info[ALL_STREAM].handle, s);
  53.     fflush(Stream_Info[ALL_STREAM].handle);
  54.   }
  55. }
  56.  
  57. /****************************************************************************/
  58. /* Use this routine to display opening banner & copyright info */
  59. int CDECL Banner(char *format,...)
  60. {
  61.   va_list marker;
  62.  
  63.   va_start(marker, format);
  64.   vsprintf(vsbuffer, format, marker);
  65.   va_end(marker);
  66.   
  67.   POV_BANNER(vsbuffer);
  68.   
  69.   return (0);
  70. }
  71.  
  72. /****************************************************************************/
  73.  
  74. /*
  75.  * Use this routine to display non-fatal warning message if
  76.  * opts.Language_Version is greater than level parameter.
  77.  */
  78. int CDECL Warning(DBL level, char *format,...)
  79. {
  80.   va_list marker;
  81.  
  82.   va_start(marker, format);
  83.   vsprintf(vsbuffer, format, marker);
  84.   va_end(marker);
  85.   
  86.   if (level >= opts.Language_Version)
  87.     return (0);
  88.   
  89.   PrintToStream(WARNING_STREAM, vsbuffer);
  90.   
  91.   if (Stream_Info[WARNING_STREAM].do_console)
  92.   {
  93.     POV_WARNING(vsbuffer);
  94.   }
  95.   
  96.   return (0);
  97. }
  98.  
  99. /****************************************************************************/
  100. /* Use this routine to display debug information. */
  101. int CDECL Debug_Info(char *format,...)
  102. {
  103.   va_list marker;
  104.  
  105.   va_start(marker, format);
  106.   vsprintf(vsbuffer, format, marker);
  107.   va_end(marker);
  108.   
  109.   PrintToStream(DEBUG_STREAM, vsbuffer);
  110.   
  111.   if (Stream_Info[DEBUG_STREAM].do_console)
  112.   {
  113.     POV_DEBUG_INFO(vsbuffer);
  114.   }
  115.   
  116.   return (0);
  117. }
  118.  
  119. /****************************************************************************/
  120.  
  121. /*
  122.  * Use this routine to display general information messages about the current
  123.  * rendering if that information applies to the entire render.  Items such as
  124.  * "Options in effect" or when animation is added it would display frame number
  125.  * or clock value. In a windowed environment this info might stay static on the
  126.  * screen during the whole session. Status messages such as "Parsing..." or
  127.  * "Building slabs, please wait" etc should use Status_Info below.
  128.  */
  129. int CDECL Render_Info(char *format,...)
  130. {
  131.   va_list marker;
  132.  
  133.   va_start(marker, format);
  134.   vsprintf(vsbuffer, format, marker);
  135.   va_end(marker);
  136.   
  137.   PrintToStream(RENDER_STREAM, vsbuffer);
  138.   
  139.   if (Stream_Info[RENDER_STREAM].do_console)
  140.   {
  141.     POV_RENDER_INFO(vsbuffer);
  142.   }
  143.   
  144.   return (0);
  145. }
  146.  
  147. /****************************************************************************/
  148.  
  149. /*
  150.  * Use this routine to display information messages such as "Parsing..." or
  151.  * "Building slabs, please wait" etc   Windowed environments might implement
  152.  * one or two status lines at the bottom of the screen.
  153.  */
  154. int CDECL Status_Info(char *format,...)
  155. {
  156.   va_list marker;
  157.  
  158.   va_start(marker, format);
  159.   vsprintf(vsbuffer, format, marker);
  160.   va_end(marker);
  161.   
  162.   POV_STATUS_INFO(vsbuffer);
  163.   
  164.   return (0);
  165. }
  166.  
  167. /****************************************************************************/
  168.  
  169. /*
  170.  * This routine is used by various specialized fatal error functions to display
  171.  * a message to the fatal error reporting device. It does not terminate
  172.  * rendering.  The function "Error" below prints a message and does the actual
  173.  * termination.  Use "Error" for most purposes.
  174.  */
  175.  
  176. int CDECL Error_Line(char *format,...)
  177. {
  178.   va_list marker;
  179.  
  180.   va_start(marker, format);
  181.   vsprintf(vsbuffer, format, marker);
  182.   va_end(marker);
  183.   
  184.   PrintToStream(FATAL_STREAM, vsbuffer);
  185.   
  186.   if (Stream_Info[FATAL_STREAM].do_console)
  187.   {
  188.     POV_FATAL(vsbuffer);
  189.   }
  190.   
  191.   return (0);
  192. }
  193.  
  194. /****************************************************************************/
  195.  
  196. /*
  197.  * This not only prints a fatal error message, it first calls Where_Error and
  198.  * it calls Terminate_POV to do the actual termination.
  199.  */
  200.  
  201. int CDECL Error(char *format,...)
  202. {
  203.   va_list marker;
  204.   
  205.   if (Stop_Flag)
  206.   {
  207.     if (POV_SHELLOUT(USER_ABORT_SHL) != FATAL_RET)
  208.     {
  209.       POV_STATUS_INFO("\nUser abort.\n");
  210.       Terminate_POV(2);
  211.     }
  212.     else
  213.     {
  214.       Error_Line("\nFatal error in User_Abort_Command.\n");
  215.     }
  216.   }
  217.  
  218.   switch (Stage)
  219.   {
  220.     case STAGE_STARTUP:
  221.       Error_Line("\nStartup error.\n");
  222.       break;
  223.  
  224.     case STAGE_BANNER:
  225.       Error_Line("\nBanner error.\n");
  226.       break;
  227.  
  228.     case STAGE_INIT:
  229.       Error_Line("\nInit error.\n");
  230.       break;
  231.  
  232.     case STAGE_ENVIRONMENT:
  233.       Error_Line("\nEnvironment error.\n");
  234.       break;
  235.  
  236.     case STAGE_COMMAND_LINE:
  237.       Error_Line("\nCommand line error.\n");
  238.       break;
  239.  
  240.     case STAGE_FILE_INIT:
  241.       Error_Line("\nFile init error.\n");
  242.       break;
  243.  
  244.     case STAGE_PARSING:
  245.     case STAGE_INCLUDE_ERR:
  246.       Where_Error();
  247.       break;
  248.  
  249.     case STAGE_CONTINUING:
  250.       Error_Line("\nContinue trace error.\n");
  251.       break;
  252.  
  253.     case STAGE_RENDERING:
  254.       Error_Line("\nRendering error.\n");
  255.       break;
  256.  
  257.     case STAGE_SHUTDOWN:
  258.       Error_Line("\nShutdown error.\n");
  259.       break;
  260.  
  261.     case STAGE_INI_FILE:
  262.       Error_Line("\nINI file error.\n");
  263.       break;
  264.  
  265.     case STAGE_CLEANUP_PARSE:
  266.       Error_Line("\nCleanup parse error.\n");
  267.       break;
  268.  
  269.     case STAGE_SLAB_BUILDING:
  270.       Error_Line("\nSlab building error.\n");
  271.       break;
  272.  
  273.     case STAGE_TOKEN_INIT:
  274.       Error_Line("\nScene file parser initialization error.\n");
  275.       break;
  276.  
  277.     case STAGE_FOUND_INSTEAD:
  278.       break;
  279.  
  280.     default:
  281.       Error_Line("\nUnkown error %d.\n", Stage);
  282.       break;
  283.   }
  284.  
  285.   va_start(marker, format);
  286.   vsprintf(vsbuffer, format, marker);
  287.   va_end(marker);
  288.   
  289.   PrintToStream(FATAL_STREAM, vsbuffer);
  290.   
  291.   if (Stream_Info[FATAL_STREAM].do_console)
  292.   {
  293.     POV_FATAL(vsbuffer);
  294.   }
  295.  
  296.   /* This could be just an "if" but we may add special messages later */
  297.  
  298.   switch (Stage)
  299.   {
  300.     case STAGE_INCLUDE_ERR:
  301.       Error_Line("Check that the file is in a directory specifed with a +L switch\n");
  302.       Error_Line("or 'Library_Path=' .INI item.  Standard include files are distributed with \n");
  303.       Error_Line("the documentation archive.  Please read POVINF.DOC carefully to make sure\n");
  304.       Error_Line("you have all required files!\n");
  305.   }
  306.  
  307.   Terminate_Tokenizer(); /* Closes scene file */
  308.  
  309.   POV_SHELLOUT(FATAL_SHL);
  310.  
  311.   Terminate_POV(1);
  312.   
  313.   return (0);
  314. }
  315.  
  316. /****************************************************************************/
  317. /* Use this routine to display final rendering statistics */
  318. int CDECL Statistics(char *format,...)
  319. {
  320.   va_list marker;
  321.  
  322.   va_start(marker, format);
  323.   vsprintf(vsbuffer, format, marker);
  324.   va_end(marker);
  325.   
  326.   PrintToStream(STATISTIC_STREAM, vsbuffer);
  327.   
  328.   if (Stream_Info[STATISTIC_STREAM].do_console)
  329.   {
  330.     POV_STATISTICS(vsbuffer);
  331.   }
  332.   
  333.   return (0);
  334. }
  335.  
  336. /****************************************************************************/
  337. /* Initialization for streams structure */
  338.  
  339. void Init_Text_Streams()
  340. {
  341.   int i;
  342.   
  343.   vsbuffer = POV_MALLOC(1000,"text buffer");
  344.  
  345.   for (i = 0; i < MAX_STREAMS; i++)
  346.   {
  347.     Stream_Info[i].handle = NULL;
  348.     Stream_Info[i].name = NULL;
  349.     Stream_Info[i].do_console = TRUE;
  350.   }
  351. }
  352.  
  353. /****************************************************************************/
  354. /* Opens stream text output files if necessary. */
  355.  
  356. void Open_Text_Streams()
  357. {
  358.   int i;
  359.  
  360.   for (i = 0; i < MAX_STREAMS; i++)
  361.   {
  362.     if (Stream_Info[i].name != NULL)
  363.     {
  364.       if (opts.Options & CONTINUE_TRACE)
  365.       {
  366.         if ((Stream_Info[i].handle =
  367.          fopen(Stream_Info[i].name, APPEND_FILE_STRING)) == NULL)
  368.         {
  369.           Warning(0.0, "Couldn't append stream to file %s.\n", Stream_Info[i].name);
  370.         }
  371.       }
  372.       else
  373.       {
  374.         if ((Stream_Info[i].handle =
  375.          fopen(Stream_Info[i].name, WRITE_FILE_STRING)) == NULL)
  376.         {
  377.           Warning(0.0, "Couldn't write stream to file %s.\n", Stream_Info[i].name);
  378.         }
  379.       }
  380.     }
  381.   }
  382. }
  383.  
  384. void Destroy_Text_Streams()
  385. {
  386.   int i;
  387.  
  388.   for (i = 0; i < MAX_STREAMS; i++)
  389.   {
  390.     if (Stream_Info[i].name)
  391.     {
  392.       POV_FREE(Stream_Info[i].name);
  393.  
  394.       Stream_Info[i].name = NULL;
  395.     }
  396.   }
  397.   POV_FREE(vsbuffer);
  398. }
  399.  
  400.  
  401. /****************************************************************************/
  402. void POV_Std_Banner(s)
  403. char *s;
  404. {
  405.   if (!Display_Started)
  406.   {
  407.   fprintf(stderr, s);
  408.   fflush(stderr);
  409.   }
  410. }
  411.  
  412. /****************************************************************************/
  413. void POV_Std_Warning(s)
  414. char *s;
  415. {
  416.   if (!Display_Started)
  417.   {
  418.   fprintf(stderr, s);
  419.   fflush(stderr);
  420.   }
  421. }
  422.  
  423. /****************************************************************************/
  424. void POV_Std_Status_Info(s)
  425. char *s;
  426. {
  427.   if (!Display_Started)
  428.   {
  429.   fprintf(stderr, s);
  430.   fflush(stderr);
  431.   }
  432. }
  433.  
  434. /****************************************************************************/
  435. void POV_Std_Render_Info(s)
  436. char *s;
  437. {
  438.   if (!Display_Started)
  439.   {
  440.   fprintf(stderr, s);
  441.   fflush(stderr);
  442.   }
  443. }
  444.  
  445. /****************************************************************************/
  446. void POV_Std_Debug_Info(s)
  447. char *s;
  448. {
  449.   if (!Display_Started)
  450.   {
  451.   fprintf(stderr, s);
  452.   fflush(stderr);
  453.   }
  454. }
  455.  
  456. /****************************************************************************/
  457. void POV_Std_Fatal(s)
  458. char *s;
  459. {
  460.   if (!Display_Started)
  461.   {
  462.   fprintf(stderr, s);
  463.   fflush(stderr);
  464.   }
  465. }
  466.  
  467. /****************************************************************************/
  468. void POV_Std_Statistics(s)
  469. char *s;
  470. {
  471.   if (!Display_Started)
  472.   {
  473.   fprintf(stderr, s);
  474.   fflush(stderr);
  475.   }
  476. }
  477.  
  478. /****************************************************************************/
  479. void Terminate_POV(i)
  480. int i;
  481. {
  482.   close_all();
  483.   
  484.   mem_release_all(i == 0);
  485.  
  486.   pre_init_flag=0;
  487.   
  488.   FINISH_POVRAY(i); /* Must call exit(i) or somehow stop */
  489. }
  490.  
  491. /***************************************************************************
  492.  *
  493.  * Dummy display routines for non-graphic Unix ports
  494.  *
  495.  **************************************************************************/
  496.  
  497. static DBL Display_Width_Scale, Display_Height_Scale;
  498. static int Prev_X, Prev_Y;
  499.  
  500. /****************************************************************************/
  501. void POV_Std_Display_Init(w, h)
  502. int w, h;
  503. {
  504.   Display_Width_Scale = 78.0 / (DBL)w;
  505.   Display_Height_Scale = 24.0 / (DBL)h;
  506.   Prev_X = 0;
  507.   Prev_Y = 0;
  508.   fprintf(stderr, "\n");
  509. }
  510.  
  511. /****************************************************************************/
  512. void POV_Std_Display_Finished()
  513. {
  514.   char s[3];
  515.  
  516.   fprintf(stderr, "\007\007");
  517.   if (opts.Options & PROMPTEXIT)
  518.   {
  519.     fgets(s, 2, stdin);
  520.   }
  521. }
  522.  
  523. /****************************************************************************/
  524. void POV_Std_Display_Close()
  525. {
  526.   fprintf(stderr, "\n");
  527. }
  528.  
  529. /****************************************************************************/
  530. void POV_Std_Display_Plot(x, y, r, g, b, a)
  531. int x, y;
  532. unsigned int r, g, b, a;
  533. {
  534.   int sx = (int)(Display_Width_Scale * ((DBL)x));
  535.   int sy = (int)(Display_Height_Scale * ((DBL)y));
  536.   char I;
  537.   unsigned char G[6] = " .o*@M";
  538.  
  539.   if (sy > Prev_Y)
  540.   {
  541.     Prev_Y++;
  542.     
  543.     fprintf(stderr, "\n");
  544.     
  545.     Prev_X = 0;
  546.   }
  547.   
  548.   if (sx > Prev_X)
  549.   {
  550.     I = (int)(((DBL)r * 1.80 + (DBL)g * 3.54 + (DBL)b * 0.66) / 256.0);
  551.  
  552.     fprintf(stderr, "%c", G[(int)I]);
  553.     
  554.     Prev_X++;
  555.   }
  556. }
  557.  
  558. /****************************************************************************/
  559. void POV_Std_Display_Plot_Rect(x1, x2, y1, y2, r, g, b, a)
  560. int x1, x2, y1, y2;
  561. unsigned int r, g, b, a;
  562. {
  563.   POV_Std_Display_Plot(x1, y1, r, g, b, a);
  564. }
  565.  
  566.